package vashion.azeroth.core.manager.forest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vashion.azeroth.contant.ForestErrorEnum;
import vashion.azeroth.core.common.NeedOperateFlow;
import vashion.azeroth.core.common.NeedPermission;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.item.dao.PropertyDao;
import vashion.azeroth.core.item.pojo.PropertyMetaDO;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.pojo.common.OperateFlowEnum;
import vashion.azeroth.core.result.ApiResult;

import java.util.Date;
import java.util.List;

/**
 * Created by tingting on 15/9/1.
 * 基础属性管理
 */
@Service
public class BasePropertyManager extends BaseManagerImpl<PropertyMetaDO> {

    private final static Logger log = LoggerFactory.getLogger(BasePropertyManager.class);

    @Autowired
    private PropertyDao propertyDao;


    @Override
    public BaseDao<PropertyMetaDO> getDao() {
        return propertyDao;
    }

    @NeedPermission
    @NeedOperateFlow(operateIdArgLocation = 2, operateFlowEnum = OperateFlowEnum.FOREST)
    public ApiResult<Boolean> publishProperty(PropertyMetaDO propertyMetaDO, long operateId) {
        if (null == propertyMetaDO || StringUtils.isBlank(propertyMetaDO.getTitle())) {
            return ApiResult.newErrorResult(ForestErrorEnum.PROPERTY_ARG_ERROR.getErrorCode(), ForestErrorEnum.PROPERTY_ARG_ERROR.getErrorMessage());
        }
        fullProperty(propertyMetaDO);
        try {
            this.save(propertyMetaDO);
        } catch (Exception e) {
            log.error("BasePropertyManager.publishProperty error, propertyMetaDO=" + propertyMetaDO, e);
            return ApiResult.newErrorResult(ForestErrorEnum.PROPERTY_SYSTEM_ERROR.getErrorCode(), ForestErrorEnum.PROPERTY_SYSTEM_ERROR.getErrorMessage());
        }
        return ApiResult.newSuccessResult(true);
    }

    private void fullProperty(PropertyMetaDO propertyMetaDO) {
        propertyMetaDO.setGmtCreate(new Date());
        propertyMetaDO.setGmtModified(new Date());
    }

    public ApiResult<PropertyMetaDO> getDetail(long propertyId) {
        PropertyMetaDO propertyMetaDO;
        try {
            propertyMetaDO = this.get(propertyId);
        } catch (Exception e) {
            log.error("BasePropertyManager.get error propertyId=" + propertyId, e);
            return ApiResult.newErrorResult(ForestErrorEnum.PROPERTY_QUERY_ERROR.getErrorCode(), ForestErrorEnum.PROPERTY_QUERY_ERROR.getErrorMessage());
        }
        return ApiResult.newSuccessResult(propertyMetaDO);
    }

    public ApiResult<List<PropertyMetaDO>> getPropertyList(PropertyMetaDO propertyMetaDO) {

        try {
            List<PropertyMetaDO> propertyMetaDOList = this.find(propertyMetaDO);
            return ApiResult.newSuccessResult(propertyMetaDOList);
        } catch (Throwable e) {
            log.error("BasePropertyManager.getPropertyList error propertyMetaDO=" + propertyMetaDO, e);
            return ApiResult.newErrorResult(ForestErrorEnum.PROPERTY_QUERY_ERROR.getErrorCode(), ForestErrorEnum.PROPERTY_QUERY_ERROR.getErrorMessage());
        }
    }

    @NeedOperateFlow(operateIdArgLocation = 2, operateFlowEnum = OperateFlowEnum.FOREST)
    public int remove(PropertyMetaDO propertyMetaDO, Long loginUserId) {
        return this.remove(propertyMetaDO);
    }
}
